//
//  ActionDViewController.m
//  A段项目,漫画
//
//  Created by dlios on 15-4-30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ActionDViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "UMSocial.h"
@interface ActionDViewController ()<UITableViewDataSource,UITableViewDelegate,UMSocialUIDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)ActionDetil *act;
@property(nonatomic, retain)NSMutableArray *epArr;
@property(nonatomic, retain)UICollectionView *myCollection;
//@property(nonatomic,retain)UIView *witeView;
//@property(nonatomic,retain)UIImageView *witeImageView;
//@property(nonatomic,retain)UILabel *myLabel;
@property(nonatomic, assign)NSInteger num;
//动画
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)NSMutableArray *witeImageArr;
@property(nonatomic, retain)UIImageView *witeImageView;
@property(nonatomic, retain)UIProgressView *myProgress;
@property(nonatomic, retain)UILabel *myLabel;
@end

@implementation ActionDViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        self.act = [[ActionDetil alloc]init];
        self.epArr = [NSMutableArray array];
    }
    return self;
}
- (void)createWiteImage{
    self.witeImageArr = [NSMutableArray array];
    NSArray *names = @[@"735d4677ec7cdd522281cfbddd29ac71－1（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－2（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－3（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－4（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－5（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－6（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－7（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－8（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－9（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－10（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－11（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－12（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－13（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－14（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－15（被拖移）.tiff",
                       @"735d4677ec7cdd522281cfbddd29ac71－16（被拖移）.tiff"];
    for (NSString *name in names) {
        UIImage *image = [UIImage imageNamed:name];
        [self.witeImageArr addObject:image];
    }
    self.witeImageView.animationImages = self.witeImageArr;
    //设置播放时间
    self.witeImageView.animationDuration = 0.5;
}
- (void)createAvtivity{
    self.witeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.witeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.witeView];
    [self.witeView release];
    
    CGFloat bili = 222.0 / 271;
    self.witeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, WIDTH / 1.5 * bili)];
    self.witeImageView.center = CGPointMake(WIDTH / 2, HEIGHT / 3);
    [self.witeView addSubview:self.witeImageView];
    [self.witeImageView release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 30)];
    self.myLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + WIDTH / 1.5 * bili / 2 + 10);
    self.myLabel.text = @"loading..........";
    [self.witeView addSubview:self.myLabel];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createAvtivity];
    [self createWiteImage];
    [self creatActionDeView];
    
    [self PleaseData];
    
    [self.view bringSubviewToFront:self.witeView];
    //播放动画
    [self.witeImageView startAnimating];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger count = 0;
    for (episode *ep in self.epArr) {
        if ([ep.from_site isEqualToString:@"tudou"]) {
            count ++;
        }
    }
    
    if (count == 0 && self.num == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有可用的播放源" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
        self.num ++;
    }
    return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ChapterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    episode *ep = self.epArr[indexPath.row];
    NSString *str = ep.name;
    
    
    str = [[str componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
    
    
    if ([ep.from_site isEqualToString:@"tudou"]) {
        [cell.chapterButton setTitle:str forState:UIControlStateNormal];
    }
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    episode *ep = self.epArr[indexPath.row];
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.url = ep.mUrl;
    [self.navigationController pushViewController:webVC animated:YES];
    [webVC release];
}
#pragma mark 创建视图
-(void)creatActionDeView
{
    self.avtionDeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
//    self.avtionDeTableView.rowHeight = 240;
    [self.view addSubview:self.avtionDeTableView];
    [_avtionDeTableView release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(click)];
    
    self.avtionDeTableView.delegate = self;
    self.avtionDeTableView.dataSource = self;
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(60, 40);
    //设置间距
    flow.minimumInteritemSpacing = 20;
    flow.minimumLineSpacing = 40;
    flow.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 300) collectionViewLayout:flow];
    self.avtionDeTableView.tableFooterView = self.myCollection;
    //注册cell
    [self.myCollection registerClass:[ChapterCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    self.myCollection.backgroundColor = [UIColor whiteColor];
    [self.myCollection release];

}

-(void)click
{

    NSString *str = self.act.cover;
    UIImage *shareImage = [UIImage imageNamed:str];
    NSString *shareText = self.act.name;
    NSString *shareURL = self.act.id;
    NSString *share = [NSString stringWithFormat:@"%@ %@",shareText,shareURL];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55406a3ce0f55a7bd5002d90" shareText:share shareImage:shareImage shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil] delegate:self];
    
    
    
}
#pragma mark 协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *reuse = @"reuse";
        ActionDetilCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[ActionDetilCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        NSURL *url = [NSURL URLWithString:self.act.cover];
        [cell.picimageView sd_setImageWithURL:url];
        cell.nameLabel.text = self.act.name;
        cell.update_stringLabel.text = self.act.update_string;
        cell.publish_timeLabel.text = self.act.publish_time;
        cell.directorLabel.text = self.act.director;
        cell.characterLabel.text = self.act.character;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == 1){
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
    }else if (indexPath.row == 2 ) {
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        return cell;
    }else{
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        }
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 240;
    }else if (indexPath.row == 1){
        return 50;
    }else if (indexPath.row == 2){
        return HEIGHT - 300;
    }else{
        return 0;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 数据请求
-(void)PleaseData
{
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=getInfo&video_id=%@&uid=(null)",self.video_id];
    NSLog(@"URL   %@",str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        [self.act setValuesForKeysWithDictionary:dic];
        NSMutableArray *epArr = dic[@"episode"];
        for (NSMutableDictionary *d in epArr) {
            episode *ep = [[episode alloc] init];
            [ep setValuesForKeysWithDictionary:d];
            [self.epArr addObject:ep];
            [ep release];
        }
        self.num = 1;
        NSLog(@"DDDDD  %ld",self.epArr.count);
        [self.avtionDeTableView reloadData];
        [self.myCollection reloadData];
        NSLog(@"22222成功");
            NSLog(@"结束动画");
            [self.witeImageView stopAnimating];
            [self.view bringSubviewToFront:self.avtionDeTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"======请求失败");
        
    }];
    
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
